package com.cogent.dynamicform.form.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.cogent.business.helper.DynamicFormHelper;
import com.cogent.common.util.MapUtil;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.domain.FormDomain;

public class DynTableFormImpl {	
	public String genDynTableView(FormDomain formdomain, List formColumnDomainList, HashMap datamap) {
		if(formdomain.getDynTabletype().equals("VIEW"))
			return genDynTableInputView(formdomain, formColumnDomainList, datamap, "view", "查看");
		StringBuffer str = new StringBuffer();
		Iterator it = formColumnDomainList.iterator();
		str.append("<tr class=\"tr_head\"><td class=\"td_last\">序号</td>");
		while(it.hasNext()) {
			FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
			if(formColumnDomain.getShowtype() == 1)
				continue;
			if(!formColumnDomain.getType().equals("HIDDEN"))
				str.append("<td class=\"td_last\">").append(formColumnDomain.getCn()).append("</td>");
		}
		str.append("</tr>");
		Object obj = datamap.get(DynamicFormHelper.DYNTABLE_FORM_KEY);
		if(obj != null) {
			List curList = (List)obj;
			for(int i=0; i<curList.size(); i++) {
				HashMap curdata = (HashMap)curList.get(i);
				str.append("<tr><td class=\"td_last\" id=\"order\">").append(i+1).append("</td>");
				it = formColumnDomainList.iterator();
				int num = 0;
				while(it.hasNext()) {
					FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
					if(formColumnDomain.getShowtype() == 1)
						continue;
					if(formColumnDomain.getType().equals("HIDDEN"))
						continue;
					str.append("<td class=\"td_last\">");
					str.append(DynamicFormHelper.getShowValue(formColumnDomain, curdata));
					str.append("</td>");
				}
				str.append("</tr>");
			}
		}
		
		return str.toString();
	}
	
	public String getDynTableView(List formColumnDomainList, List viewVlaueList) {
		StringBuffer str = new StringBuffer();
		Iterator it = formColumnDomainList.iterator();
		str.append("<tr class=\"tr_head\"><td class=\"td_last\">序号</td>");
		while(it.hasNext()) {
			FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
			if(formColumnDomain.getShowtype() == 1)
				continue;
			if(!formColumnDomain.getType().equals("HIDDEN"))
				str.append("<td class=\"td_last\">").append(formColumnDomain.getCn()).append("</td>");
		}
		str.append("</tr>");
		for(int i=0; i<viewVlaueList.size(); i++) {
			HashMap curdata = (HashMap)viewVlaueList.get(i);
			str.append("<tr><td class=\"td_last\" id=\"order\">").append(i+1).append("</td>");
			it = formColumnDomainList.iterator();
			int num = 0;
			while(it.hasNext()) {
				FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
				if(formColumnDomain.getShowtype() == 1)
					continue;
				if(formColumnDomain.getType().equals("HIDDEN"))
					continue;
				str.append("<td class=\"td_last\">");
				str.append(DynamicFormHelper.getShowValue(formColumnDomain, curdata));
				str.append("</td>");
			}
			str.append("</tr>");
		}
		
		return str.toString();
	}
	
	public String genDynTableInputView(FormDomain formdomain, List formColumnDomainList, 
			HashMap formMap, String optType, String optName) {
		StringBuffer str = new StringBuffer();
		Iterator it = formColumnDomainList.iterator();
		str.append("<tr class=\"tr_head\"><td class=\"td_last\">序号</td>");
		while(it.hasNext()) {
			FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
			if(formColumnDomain.getShowtype() == 1)
				continue;
			if(!formColumnDomain.getType().equals("HIDDEN"))
				str.append("<td class=\"td_last\">").append(formColumnDomain.getCn()).append("</td>");
		}
		str.append("<td class=\"td_last\">&nbsp;&nbsp;</td></tr>");
		Object obj = formMap.get(DynamicFormHelper.DYNTABLE_FORM_KEY);
		if(obj != null) {
			List curList = (List)obj;
			for(int i=0; i<curList.size(); i++) {
				HashMap curdata = (HashMap)curList.get(i);
				str.append("<tr><td class=\"td_last\" id=\"order\">").append(i+1).append("</td>");
				it = formColumnDomainList.iterator();
				while(it.hasNext()) {
					FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
					if(formColumnDomain.getShowtype() == 1)
						continue;
					if(!formColumnDomain.getType().equals("HIDDEN"))
						str.append("<td class=\"td_last\">");
					str.append(DynamicFormHelper.getShowValue(formColumnDomain, curdata) + "&nbsp;");
					if(!formColumnDomain.getType().equals("HIDDEN"))
						str.append("</td>");
				}
				str.append("<td class=\"td_last\"><a href=\"javascript://\" onclick=\"").append(optType).append("('")
					.append(MapUtil.getStringValue(curdata, "id")).append("')\">").append(optName).append("</a></td>");
				str.append("</tr>");
			}
		}
		
		return str.toString();
	}
	
	public String genDynTableInput(FormDomain formdomain, List formColumnDomainList, HashMap formMap) {
		if(formdomain.getDynTableViewmodel() == 1)
			return genDynTableView(formdomain, formColumnDomainList, formMap);
		if(formdomain.getDynTabletype().equals("VIEW"))
			return genDynTableInputView(formdomain, formColumnDomainList, formMap, "mod", formdomain.getDyntableoperatname());
		StringBuffer str = new StringBuffer();
		if(formdomain.getDynTableViewmodel() != 2) {
			Iterator it = formColumnDomainList.iterator();
			str.append("<tr id=\"trTemplate\" style=\"display:none;\">").append("<td class=\"td_last\" id=\"order\"></td>");
			int num = 0;
			HashMap tempMap = new HashMap();
			while(it.hasNext()) {
				FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
				if(formColumnDomain.getShowtype() == 2)
					continue;
				if(!formColumnDomain.getType().equals("HIDDEN"))
					str.append("<td class=\"td_last\">");
				if(num == 0)
					str.append("<input type=\"hidden\" id=\"DYNTABLE_num_.id\" name=\"DYNTABLE_num_.id\"/>");
				if(formColumnDomain.getType().equals("PRODUCTSELECT"))
					tempMap.put(formColumnDomain.getTypecode(), MapUtil.getStringValue(formMap, formColumnDomain.getTypecode()));
				String name = formColumnDomain.getName();
				formColumnDomain.setName("DYNTABLE_num_." + formColumnDomain.getName());
				str.append(DynamicFormHelper.genInputField(formColumnDomain, tempMap));
				formColumnDomain.setName(name);
				if(!formColumnDomain.getType().equals("HIDDEN"))
					str.append("</td>");
				num++;
			}
			str.append("<td class=\"td_last\"><a href=\"javascript://\" onclick='delTr(this)'>删除</a></td></tr>");
		}
		Iterator it = formColumnDomainList.iterator();
		str.append("<tr class=\"tr_head\"><td class=\"td_last\">序号</td>");
		while(it.hasNext()) {
			FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
			if(formColumnDomain.getShowtype() == 2)
				continue;
			if(!formColumnDomain.getType().equals("HIDDEN"))
				str.append("<td class=\"td_last\">").append(formColumnDomain.getCn()).append("</td>");
		}
		if(formdomain.getDynTableViewmodel() != 2) 
			str.append("<td class=\"td_last\"><img src=\"images/btn_add2.gif\" onclick=\"addTr()\" style=\"cursor: hand\" border=\"0\" align=\"absmiddle\"/></td></tr>");
		Object obj = formMap.get(DynamicFormHelper.DYNTABLE_FORM_KEY);
		formMap.remove(DynamicFormHelper.DYNTABLE_FORM_KEY);
		if(obj != null) {
			List curList = (List)obj;
			for(int i=0; i<curList.size(); i++) {
				HashMap curdata = (HashMap)curList.get(i);
				MapUtil.copyChangeMap(formMap, curdata);
				str.append("<tr><td class=\"td_last\" id=\"order\">").append(i+1).append("</td>");
				it = formColumnDomainList.iterator();
				int num = 0;
				while(it.hasNext()) {
					FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
					if(formColumnDomain.getShowtype() == 2)
						continue;
					if(!formColumnDomain.getType().equals("HIDDEN"))
						str.append("<td class=\"td_last\">");
					if(num == 0)
						str.append("<input type=\"hidden\" name=\"DYNTABLE[").append(i).append("].id\" value=\"")
							.append(MapUtil.getStringValue(curdata, "id")).append("\"/>");
					String name = formColumnDomain.getName();
					formColumnDomain.setName("DYNTABLE[" + i + "]." + formColumnDomain.getName());
					str.append(DynamicFormHelper.genInputField(formColumnDomain, curdata));
					formColumnDomain.setName(name);
					if(!formColumnDomain.getType().equals("HIDDEN"))
						str.append("</td>");
					num++;
				}
				if(formdomain.getDynTableViewmodel() != 2) 
					str.append("<td class=\"td_last\"><a href=\"javascript://\" onclick='delTr(this)'>删除</a></td>");
				str.append("</tr>");
			}
		}
		
		return str.toString();
	}
}
